var objectdata = {next:0,prev:0,current:0,selectname:""};
var objectModel = {};
$(function(){
	init();
}); 

function init(){
	init_btn();
	init_dialog();
	pageaction("init");
}

init_btn = function(){
	$(".thumbnails li img").live("click",function(){
		var id = $(this).attr("data-id");
		var name = $(this).attr("data-name");
		pageaction("next",id,name);
	});
	$(".span3").live("mouseenter",function(){
		$(this).css("background","#999");
		$(this).find("div").css("color","white");
	});
	$(".span3").live("mouseleave",function(){
		$(this).css("background","white");
		$(this).find("div").css("color","#555");
	});
	
	$(".backbtn").click(function(){
		pageaction("back");
	});
	$(".firstbtn").click(function(){
		pageaction("first");
	});
}

load = function(request_id,action){
	var menu = {};
	btn = function(data){
		var btns = '<div class="btn-group captionbtns" data-toggle="buttons-radio">';
		btns += "<a class='btn btn-info' onclick='get(this)' data-desc='"+data.description+"' data-id='"+data.id+"' data-act='update'>编辑</a>";
		btns += "<a class='btn btn-info' onclick='del(this)' data-desc='"+data.description+"' data-id='"+data.id+"' data-act='update'>删除</a>";
		btns += "</div>";
		return btns;
	}
		
	effect("show");
	cb = function(data){
		effect("hide");
		if(data.code==1){
			if(data.data.length!=0){
				$(".object_list .thumbnails").empty();
				$.each(data.data,function(index,value){
					menu.img 		= factory("img",site.img+"weixin.png",data.data[index]);
					menu.desc 		= factory("h3",data.data[index].description);
					menu.content 	= factory("p",data.data[index].content);
					menu.btngroup 	= factory("p",btn(data.data[index]));
					menu.caption 	= factory("caption",menu.desc+menu.content+menu.btngroup);
					menu.thumbnail 	= factory("thumbnail",menu.img+menu.caption);
					menu.li 		= factory("li",menu.thumbnail,data.data[index]);
					$(".object_list .thumbnails").append(menu.li);	
				});
				objectdata.current = data.data[0].parent_id;
			}else{
				msg("没有子类信息");
			}
		}
	}
	var json = {url:site.web+site.module+"/list",uid:site.uid,pid:request_id,current:objectdata.current,act:action};
	G(json,cb);
}

factory = function(type,value,jsondata){
	var htm = '';
	switch(type){
		case "li":
			htm = '<li class="span3" >'+value+'</li>';
			break;
		case "img":
			htm = '<img data-src="holder.js/300x200" alt="300x200" style="width: 300px; height: 200px;border:0;marign:0;padding:0;" class="current" src="'+value+'" data-id="'+jsondata.id+'" data-pid="'+jsondata.parent_id+'" data-name="'+jsondata.description+'">';
			break;
		case "thumbnail":
			htm = '<div class="thumbnail">'+value+'</div>';
			break;
		case "caption":
			htm = '<div class="caption">'+value+'</div>';
			break;
		case "h3":
			htm = '<h3>'+value+'</h3>';
			break;
		case "p":
			htm = '<p>'+value+'</p>';
			break;
		case "create":
			htm = "";
			break;
	}
	return htm;
}

effect = function(action){
	switch(action){
		case "show":$(".customajax").show();break;
		case "hide":$(".customajax").hide();break;
	}
}

pageaction = function(act,id,name){
	switch(act){
		case "init":
		case "first":
			objectdata.next = 0;
			objectdata.name = "";
			break;
		case "refresh":
			objectdata.current = id;
			load(id,act);
			return;
			break;
		case "back":
			break;
		case "next":			
			objectdata.next = id;
			objectdata.name = name;
			break;
	}
	load(objectdata.next,act);	
}

msg = function(message){
	$(".msg").text(objectdata.name+message);
	$(".msg").show();
	h = function(){
		$(".msg").hide();
	}
	setTimeout(h,"2000");
}

// dialog method ---------------------------------------------

init_dialog = function(){
	$("#dialog_create").click(function(){//create & open dialog
		objectModel.edit_status = "create";
		open_dialog();
	});
	$("#dialog_save").click(function(){
		$(this).attr("disabled",true);
		$(".dialogmsg").text("执行操作中...");
		objectModel.uid = site.uid;
		objectModel.url = site.web+site.module+"/"+objectModel.edit_status;//包含create/update操作
		objectModel.description = $("#description").val();				
		objectModel.content = $(".content").val();				
		if(objectModel.description==""){
			$(".dialogmsg").text("名称为空!");
			return;
		}
		if(objectModel.content==""){
			$(".dialogmsg").text("内容为空!");
			return;
		}
		objectModel.objectid =  objectModel.id;//update value
		complete = function(data){
			done_method = function(){
				$('#myModal').modal('hide');
				pageaction("refresh",objectModel.id,objectModel.description);
			};
			if(data.code==1){
				$(".dialogmsg").text("操作成功!");
				setTimeout(done_method, 2000);
				
			}else{
				$('#myModal').modal('hide');
			}
			
		}
		G(objectModel,complete);
	});	
	
	$(".canceldialog").click(function(){//cancel
		clear_dialog();
		$('#myModal').modal('hide');
	});
	$(".canceldialog2").click(function(){//cancel
		$('#myModal2').modal('hide');
		$('#myModal').modal('show');
	});
}

clear_dialog = function(){
	$("#form2 input[type=reset]").click();
	$("#dialog_save").attr("disabled",false);
	$(".dialogmsg").text("");
}

open_dialog = function(data){
	clear_dialog();
	switch(objectModel.edit_status){
		case "create":
			$(".dialog_status").text("新增");
			break;
		case "update":
			$(".dialog_status").text("更新");
			
			var parentName = "主菜单";
			//data[0]:子菜单,data[1]:父菜单
			objectModel = data[0];
			objectModel.uid = site.uid;
			objectModel.edit_status = "update";
			if(data.length==2){
				parentName = data[1].description;
			}
			$("#pname").val(parentName);
			$("#pname").attr("disabled",true);
			$("#description").val(objectModel.description);
			$(".content").val(objectModel.content);
			break;
	}
	$('#myModal').modal('show');
}

get = function(arg){
	var id = $(arg).attr("data-id");
	cb = function (data){
		if(data.code==1){
			objectModel.edit_status = "update";
			open_dialog (data.data);
		}else{
			alert("ERROR custom 404");
		}
	}
	var url = site.web+site.module+"/get";
	var json = {url:url,objectid:id,uid:site.uid};
	G(json,cb);
}

del = function(arg){
	var name =  $(arg).attr("data-desc");
	if(confirm("确认移除"+name+"?")==false)return;
	var id = $(arg).attr("data-id");
	cb = function (data){
		mpage.init();
	}
	var url = site.web+site.module+"/del";
	var json = {url:url,gid:id,uid:site.uid};
	G(json,cb);
}



